using System;
using System.Net.Sockets;
using System.Net;
using System.Threading;
using System.IO;
using System.Collections;
using timers = System.Timers;
using System.Collections.Generic;
using System.Xml;

namespace Thinxoft.HttpLib
{
	
	/// <summary>
	/// handles string display messages
	/// </summary>
	public delegate void DisplayEventHandler(object info);


	
	public class HttpApplication :  MarshalByRefObject, IHttpServer
	{
		
		List<string> defaultDocs;
		HttpApplicationContext application;		
		#region [constants]
		const string VERSION = "Thinxoft P2P Http Server [Early Adopters] version 1.0";
		public const string XFile_BIN = "./xfiles/";		
		public const string DATA_FOLDER = "./data/";
        public const string GLOBAL_SETTINGS_FILE = "./data/xedusconfig.xml";
		#endregion		
		public event DisplayEventHandler display;
        //private Dictionary<string, string> xFiles = new Dictionary<string, string>();       
        Settings settings;
        XmlNode state;
        AppDomain domain;
        string globalRoot;
        bool directoryBrowsing = false;


        public HttpApplication(XmlNode app, Settings settings)
        {
           
            this.settings = settings;
            this.state = app;
                        
            //setup default documents
            this.defaultDocs = new List<string>() ;
            foreach (XmlNode doc in app["defaultdocs"])
            {
                this.defaultDocs.Add(doc.InnerText);
            }

            domain = AppDomain.CreateDomain("domain" + Name);
            this.application = new HttpApplicationContext();
            globalRoot = settings.Configuration["applications"].Attributes["globalroot"].Value ;
            bool.TryParse(app["directorybrowsing"].InnerText, out directoryBrowsing);
        }

        public string GlobalRoot
        {
            get
            {
                return this.globalRoot;
            }
        }

        

       

        public bool IsHostApp(string path)
        {
            if (path.Contains(path))
                return true;
            else
                return false;
        }

        public void CreateSession(string sessionId)
        {
            HttpSession session = new HttpSession();
            session.SetId(sessionId);
            this.Application.Add(sessionId, session);
        }
     


		#region [public  properties]

        public AppDomain ApplicationDomain
        {
            get
            {
                return this.domain;
            }
        }

        public Settings ApplicationSettings
        {
            get
            {
                return this.settings;
            }
        }
        public string Name
        {
            get
            {
                return state["name"].InnerText.ToLower();
            }
            set
            {
                state["name"].InnerText = value;
            }
        }

        /// <summary>
        /// document root of the server
        /// </summary>
        public string DocumentRoot
        {
            get { return state["root"].InnerText; }
            set { state["root"].InnerText = value; }
        }

        public bool DirectoruBrowsing
        {
            get
            {
                return directoryBrowsing;
            }
        }


        public List<string> DefaultDocuments
		{
			get{return this.defaultDocs;}
		}

		public IHttpApplicationContext Application
		{
			get{return this.application;}
		}
        
		#endregion		

       

        internal void Display(string s)
		{
			//fire display event
			if(display != null)
				display(s);		
		}//end method

		internal void Display(string format, params string[] args)
		{
			string str = string.Format(format,args);
			Display(str);
		}

				

       
    }
}
